{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9ee58192-8869-4e61-899a-c6bca9bdcac1",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/lru-cache\n",
    "\n",
    "\n",
    "Runtime: 2956 ms, faster than 5.00% of Python3 online submissions for LRU Cache.\n",
    "Memory Usage: 74.2 MB, less than 36.60% of Python3 online submissions for LRU Cache.\n",
    "\n",
    "\n",
    "```python\n",
    "class LRUCache:\n",
    "\n",
    "    def __init__(self, capacity: int):\n",
    "        #3:39\n",
    "        self.history = []\n",
    "        self.dict_ = {}\n",
    "        self.capacity = capacity\n",
    "        #3:45\n",
    "        #debug until 4:04\n",
    "\n",
    "    def get(self, key: int) -> int:\n",
    "        #print(self.history)\n",
    "        if key in self.dict_:\n",
    "            self.history.remove(key)\n",
    "            self.history.append(key)\n",
    "            return self.dict_[key]\n",
    "        else:\n",
    "            return -1\n",
    "\n",
    "    def put(self, key: int, value: int) -> None:\n",
    "        if key in self.dict_:\n",
    "            self.history.remove(key)\n",
    "            self.history.append(key)\n",
    "            self.dict_[key] = value\n",
    "            return None\n",
    "        else:\n",
    "            if len(self.history) >= self.capacity:\n",
    "                old_key = self.history[0]\n",
    "                self.history = self.history[1:]\n",
    "                del self.dict_[old_key]\n",
    "            self.dict_[key] = value\n",
    "            self.history.append(key)\n",
    "            return None\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f7ca7183-d323-40fb-8aa2-bb961ec9a7b4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
